package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;


    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {

        //参数为空
        if (dto == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        dto.checkParam();

        QueryWrapper<ApUserRealname> wrapper = new QueryWrapper<>();
        wrapper.eq("status", dto.getStatus());

        //wrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());

        //LambdaQueryWrapper<ApUserRealname> wrapper1 =new LambdaQueryWrapper<>();
        //wrapper1.eq(ApUserRealname::getStatus,dto.getStatus());

        //List<ApUserRealname> apUserRealnameList = list(wrapper);

        Page page = new Page(dto.getPage(), dto.getSize());
        IPage iPage = page(page, wrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        responseResult.setData(iPage.getRecords());

        return responseResult;
    }

    @Override
    @GlobalTransactional  //全局事务注解
    public ResponseResult updateStatusById(AuthDto dto, Short status) {


        //1.检查参数
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (statusCheck(status)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //审核  修改审核状态   自媒体   作者(findByName   save)

        //1:修改 待审核列表的状态  ap_user_realname
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (dto.getMsg() != null) {
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);

        //2: 自媒体   作者   审核通过 ：9
        if(status.equals((short)9)){
            ResponseResult result = this.createWmuserAndAuthor(dto);

            int i = 10/0;

            if(result.getCode()!=null && result.getCode()==100){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    private ResponseResult createWmuserAndAuthor(AuthDto dto){
        // 自媒体
        ApUserRealname apUserRealname = this.getById(dto.getId());
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if(apUser ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser == null || wmUser.getId()==null) {
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }

        //作者
        Integer wmUserId = wmUser.getId();
        Integer apUserId = wmUser.getApUserId();

        ApAuthor apAuthor = articleFeign.findByUserId(apUserId);
        if(apAuthor ==null){
            apAuthor = new ApAuthor();
            apAuthor.setName(apUser.getName());
            apAuthor.setType(2); // 2：作者
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            apAuthor.setWmUserId(wmUserId);
            articleFeign.save(apAuthor);
        }

        //修改 apuser  成为自媒体状态
        apUser.setFlag((short)1);//1:自媒体人
        apUserMapper.updateById(apUser);

       return ResponseResult.okResult(100,"SUCC");
    }

    private boolean statusCheck(Short status) {

        if (status == null || (status != 9 && status != 2)) {
            return true;
        }
        return false;
    }
}
